﻿using System;
using System.Data;

namespace ALDIOrganizer
{
    public static class DataRowExtensionMethods
    {
        /// <summary>
        /// does the data contained in the row match length and datatypes of the given column collection
        /// </summary>
        /// <param Name="row">non-Empty DataRow (filled up with data)</param>
        /// <param Name="columns">columns from DataTable</param>
        /// <returns>is HolidayTypesDataSource of datarow consistent with datacolumn</returns>
        public static bool MatchesScheme(this DataRow row, DataColumnCollection columns)
        {
            if (row.ItemArray.Length != columns.Count)
            {
                return false;
            }
            var column = 0;
            foreach (var o in row.ItemArray)
            {
                if (o == DBNull.Value)
                {
                    if (!columns[column++].AllowDBNull)
                    {
                        return false;
                    }
                }
                else
                {
                    if (o.GetType() != columns[column++].DataType)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static string FullDescription(this DataRow row, bool role, bool hours, bool holidays)
        {
            var result = String.Empty;
            var date = (DateTime)row[2];
            var pos = ((Int64)row[4]).To<Position>();
            var hrs = (Int64)row[5];
            var hols = (Int64)row[6];

            result += date.ToShortDateString();
            result += ": ";

            if ((bool)row[3]) // Hired
            {
                if (role) result += pos.ToShortString() + "; ";
                if (hours) result += hrs.ToString() + " hrs; ";
                if (holidays) result += hols.ToString() + " holidays; ";
            }
            else result += "terminated"; // not Hired

            if (result.Substring(result.Length - 2, 2) == "; ")
                result = result.Remove(result.Length - 2, 2);

            return result;
        }
    }
}
